package bootstrap

import (
	"context"
	"net/http"
	"sync"

	"gitee.com/pangxianfei/frame/facades"
	"gitee.com/pangxianfei/frame/http/middleware"
	"gitee.com/pangxianfei/frame/kernel/zone"
	"gitee.com/pangxianfei/frame/library/config"
	"gitee.com/pangxianfei/frame/library/tmaic"
	"gitee.com/pangxianfei/frame/request"
	"github.com/gin-gonic/gin"

	"tmaic/login/routes"
)

func (s *App) LoginHttpServe(parentCtx context.Context, wg *sync.WaitGroup) *http.Server {
	//gin.SetMode(gin.ReleaseMode)
	//gin.DefaultWriter = ioutil.Discard
	r := request.New()
	r.Use(middleware.RequestLogger())
	r.Use(middleware.Logger())
	r.Use(middleware.Recovery())
	r.Use(middleware.Locale())
	routes.Register(r)
	r.GET("/", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{"code": http.StatusOK, "error": "Welcome loginHttpServer"})
	})
	server := &http.Server{
		Addr:           config.Instance.AppPort.LoginPort,
		Handler:        r,
		ReadTimeout:    zone.Duration(config.GetInt64("app.read_timeout_seconds")) * zone.Second,
		WriteTimeout:   zone.Duration(config.GetInt64("app.write_timeout_seconds")) * zone.Second,
		MaxHeaderBytes: 1 << 20,
	}
	facades.Log.Info("LoginHttpServe At", tmaic.V{"Addr": server.Addr})

	return server
}
